/**
 * Created by Administrator on 2018/6/4 0004.
 */
const Shops = require('../model/shop.model')
const config = require('../config')
const {handleError, handleSuccess} = require('../units/handle')
class ShopsController {
    static async addShop(ctx) {
        const data = await Shops.create(ctx.request.body).catch(e => {
            console.log(e)
        })
        console.log(data)
        if (data) {
            handleSuccess({ctx, message: "添加成功"})
        } else {
            handleError({ctx, message: "添加失败"})
        }
    }

    static async getShops(ctx) {
        const {page, type, size} = ctx.query;
        const options = {
            page: Number(page),
            size: Number(size),
            // select: 'name date author',//选择需要字段筛选
            sort: {date: 1},
            // populate: 'author',
            // lean: true,
            // offset: 20,
            limit: 10
        }
        const query = {type}
        const data = await Shops.paginate(query, options).catch(e => {
            console.log(e)
        })

        if (data) {
            handleSuccess({ctx, data})
        } else {
            handleError({ctx})
        }
    }

    static async deleteShop(ctx) {
        const {_id} = ctx.query;
        const data = await Shops.remove({_id}).catch(e => {
            console.log(e)
        })
        if (data) {
            handleSuccess({ctx, message: "删除成功"})
        } else {
            handleError({ctx, message: "删除失败"})
        }
    }

    static async shopParticuler(ctx) {
        const {_id} = ctx.query;
        const data = await Shops.findById(_id).catch(e => {
            console.log(e)
        })
        if (data) {
            handleSuccess({ctx, message: "查询成功", data})
        } else {
            handleError({ctx, message: "未查询到该数据"})
        }
    }

    static async updateShop(ctx) {
        const {_id} = ctx.request.body;
        delete ctx.request.update_time;
        const data = await Shops.update({_id}, ctx.request.body).catch(e => {
            console.log(e)
        })
        if (data) {
            handleSuccess({ctx, message: "更新成功"})
        } else {
            handleError({ctx, message: "更新失败"})
        }
    }
}
module.exports = ShopsController